package analizator;

import analizator.Analizator.Atom;
import java.util.ArrayList;

/**
 *
 * @author bufu
 */
public class AtomProvider {

    private ArrayList<Atom> m_atomi;
    private int m_index;

    public AtomProvider(Analizator analiz) {
        Atom temp;
        m_atomi = new ArrayList<>();
        while ((temp = analiz.debugGetNextAtom()) != null) {
            m_atomi.add(temp);
        }
        m_index = 0;
    }

    public void revertOne() {
        if (m_index > 0) {
            m_index--;
        }
    }

    public int getCurrentIndex() {
        return m_index;
    }

    public void setIndex(int index) {
        if (index > 0) {
            m_index = index;
        }
    }

    @Deprecated
    public void putUnusedAtom(Atom a) {
        if (m_index > 0) {
            m_index--;
        } else {
            System.err.println("Rollback failed");
        }
    }

    @Deprecated
    public void putUnusedAtoms(ArrayList<Atom> a) {
        if (a == null) {
            return;
        }
        if (a.size() > m_index) {
            m_index -= a.size();
        } else {
            System.err.println("MultiRollback failed");
        }
    }

    public Atom debugGetNextAtom() {
        Atom a = null;
        if (m_atomi.size() > m_index) {
            a = m_atomi.get(m_index++);
        }
        if (a != null) {
            System.out.println(a);
        } else {
            System.err.println("No next atom!,index:" + m_index);
        }
        return a;
    }
}
